<template>
	<view class="container">
		<block v-if="tid == null">
			<view class="title app_title_color">选择版块</view>
			<view class="sub_board_content grid-content">
				<block v-for="(item,index) in subBoardList">
					<view class="sub_board_cell_select_content" v-if="selectBoard && selectBoard.board_id == item.board_id">
						{{item.board_name}}
					</view>
					<view class="sub_board_cell_content" v-else @tap="selectBoardTap(item)">{{item.board_name}}</view>
				</block>
			</view>
			<view class="title app_title_color">输入标题</view>
			<view class="textarea_content app_content_color">
				<input class="input app_title_color" placeholder="点击输入帖子标题" @input="titleInput"></input>
			</view>
		</block>
		<view class="title app_title_color">输入内容</view>
		<view class="textarea_content app_content_color">
			<textarea class="textarea app_title_color" :value="forumContent" :placeholder="rName == null?'点击输入帖子内容':('点击回复'+rName)" @input="contentInput"></textarea>
			<view class="row-content">
				<image class="reply_emoji_button" mode="aspectFit" src="../../static/emoji_icon.png" @tap="emojiTap"></image>
			</view>
		</view>
		<scroll-view scroll-x class="image_scroll_content">
			<view class="row-content" style="margin-left: 30upx;">
				<image class="image" mode="aspectFill" v-for="(item,index) in images" :src="item.urlName"
					@tap="imageTap(index)"></image>
				<image class="image" mode="aspectFit" src="../../static/add_image.png" @tap="imageAddTap"></image>
			</view>
		</scroll-view>
		<view class="submit_button_content" @tap="submitTap">{{tid == null?"发表帖子":"回复"}}</view>
		
		<uni-popup ref="emojiDialog" type="bottom">
			<emoji-view v-on:emojiTap="emojiInput"></emoji-view>
		</uni-popup>
	</view>
</template>

<script>
	import http from "../../utils/http.js"
	export default {
		data() {
			return {
				fNmae: "",
				fid: "",
				tid:null,
				rid:null,
				rName:null,
				subBoardList: [],
				forumTitle: "",
				forumContent: "",
				selectBoard: null,
				images: [],
				showEmoji:true
			}
		},
		onLoad(options) {
			console.log(options)
			this.fNmae = options.fNmae
			this.fid = options.fid
			this.tid = options.tid
			this.rid = options.rid
			this.rName = options.rName
			if(this.tid == null){
				this.loadData()
				uni.setNavigationBarTitle({
					title:"写帖子"
				})
			}else{
				uni.setNavigationBarTitle({
					title:"写回复"
				})
			}
		},
		methods: {
			loadData() {
				let that = this;
				http.request("forum/forumlist", {
					fid: this.fid
				}, {
					success: function(data) {
						if (data.list.length > 0) {
							that.subBoardList = data.list[0].board_list
						} else {
							that.subBoardList = [{
								board_name: that.fNmae,
								board_id: that.fid
							}]
						}
					},
					fail: function(msg) {
						uni.showToast({
							icon: "none",
							title: msg
						})
					}
				})
			},
			selectBoardTap(item) {
				this.selectBoard = item
			},
			titleInput(e) {
				this.forumTitle = e.detail.value
			},
			contentInput(e) {
				this.forumContent = e.detail.value
			},
			emojiTap(){
				this.$refs.emojiDialog.open()
			},
			emojiInput(emoji){
				this.forumContent = this.forumContent+emoji.alt
			},
			imageAddTap() {
				let that = this
				uni.chooseImage({
					count: 1,
					sizeType:["compressed"],
					success(res) {
						let filePath = res.tempFilePaths[0]
						uni.showLoading({
							title:"上传中..."
						})
						uniCloud.uploadFile({
							filePath:filePath,
							cloudPath: Date.now() + '.jpg',
							success(res) {
								that.images.push({
									urlName:res.fileID
								})
								uni.hideLoading()
							},
							fail() {
								uni.showToast({
									icon:"none",
									title:"上传失败"
								})
								uni.hideLoading()
							}
						})
						// uni.showLoading({
						// 	title:"检测中..."
						// })
						// wx.getFileSystemManager().readFile({
						// 	filePath:filePath,
						// 	success(buffer){
						// 		wx.cloud.callFunction({
						// 			name: 'checkMsg',
						// 			data: {
						// 				"action": "imgSecCheck",
						// 				"content": buffer.data
						// 			}
						// 		}).then(res => {
						// 			uni.hideLoading()
						// 			if (res.result.code == 200) {
						// 				uni.showLoading({
						// 					title:"上传中..."
						// 				})
						// 				uniCloud.uploadFile({
						// 					filePath:filePath,
						// 					cloudPath: Date.now() + '.jpg',
						// 					success(res) {
						// 						that.images.push({
						// 							urlName:res.fileID
						// 						})
						// 						uni.hideLoading()
						// 					},
						// 					fail() {
						// 						uni.showToast({
						// 							icon:"none",
						// 							title:"上传失败"
						// 						})
						// 						uni.hideLoading()
						// 					}
						// 				})
						// 			}else{
						// 				uni.showToast({
						// 					icon:"none",
						// 					title:"图片存在违规内容"
						// 				})
						// 			}
						// 		})
						// 	}
						// })
					}
				})
			},
			imageTap(index) {
				let that = this
				uni.showActionSheet({
					itemList: ["删除", "查看大图"],
					success(res) {
						if (res.tapIndex == 0) {
							that.images.splice(index)
						} else if (res.tapIndex == 1) {
							uni.previewImage({
								urls: [that.images[index].urlName]
							})
						}
					}
				})
			},
			sendWXMsg(replyText, userId) {
				return;
				//#ifdef MP-WEIXIN
			
				let that = this
				uni.getStorage({
					key: 'uni_user_data',
					success: function(res) {
						var user = res.data;
						if (user.uid == userId) {
							return
						}
						let data = {
							uid: userId + "",
							title: that.post.title,
							nickname: user.userName,
							content: replyText,
							time: util.getDatetime(),
							topicId: that.post.topic_id
						}
						wx.cloud.callFunction({
							name: 'sendMsg',
							data: data
						}).then(res => {
			
						})
					},
					fail: function() {
						uni.showToast({
							title: "获取用户信息失败",
							icon: "none"
						})
					}
				});
			
				//#endif
			},
			submitTap() {
				if (this.selectBoard == null && this.tid == null) {
					uni.showToast({
						icon: "none",
						title: "请选择帖子版块"
					})
					return
				}
				if (this.forumTitle.length == 0 && this.tid == null) {
					uni.showToast({
						icon: "none",
						title: "请输入帖子标题"
					})
					return
				}
				if (this.forumContent.length == 0) {
					uni.showToast({
						icon: "none",
						title: this.tid == null?"请输入帖内容":"请输入回复内容"
					})
					return
				}
				
				var contents = []
				this.forumContent.split("\n").map(function(item){
					contents.push({
								type: 0,
								infor: item
							})
				})
				this.images.map(function(item){
					contents.push({
								type: 1,
								infor: item.urlName
							})
				})
				
				var jsonData = {
					body: {
						json: {
							isAnonymous: 0,
							isOnlyAuthor: 0,
							isQuote:  this.rid == null?0:1,
							typeId: this.selectBoard == null?"":this.selectBoard.board_id,
							title: this.forumTitle,
							aid: '',
							fid: this.fid == null?"":this.fid,
							tid: this.tid == null?"":this.tid,
							replyId: this.rid == null?"":this.rid,
							content: JSON.stringify(contents)
						}
					}
				};
				var json = JSON.stringify(jsonData);
				
				let that = this
				http.checkMsgText(this.forumTitle,{
					success(){
						http.checkMsgText(this.forumContent,{
							success(){
								http.request("forum/topicadmin", {
									act: that.tid != null?"reply":"new",
									platType:getApp().globalData.platform,
									json:json
								}, {
									success: function(data) {
										if(that.tid == null){
											uni.reLaunch({
												url:"../home/home"
											})
										}else{
											uni.navigateBack({
												
											})
										}
									},
									fail: function(msg) {
										uni.showToast({
											icon: "none",
											title: msg
										})
									}
								})
							}
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.title {
		font-size: 30upx;
		margin-top: 30upx;
		margin-left: 30upx;
	}

	.sub_board_content {
		margin-left: 30upx;
	}

	.sub_board_cell_content {
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid $uni-global-green-color;
		color: $uni-global-green-color;
		font-size: 30upx;
		height: 60upx;
		border-radius: 6upx;
		margin-right: 30upx;
		margin-top: 20upx;
		padding-left: 30upx;
		padding-right: 30upx;
		white-space: nowrap;
	}

	.sub_board_cell_select_content {
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: $uni-global-green-color;
		color: #fff;
		font-size: 30upx;
		height: 60upx;
		border-radius: 6upx;
		margin-right: 30upx;
		margin-top: 20upx;
		padding-left: 30upx;
		padding-right: 30upx;
		white-space: nowrap;
	}

	.textarea_content {
		display: flex;
		border-radius: 10upx;
		margin-top: 30upx;
		margin-left: 30upx;
		margin-right: 30upx;
		flex-direction: column;
	}

	.textarea {
		margin: 30upx;
		width: 100%;
		height: 300upx;
		font-size: 30upx;
	}

	.input {
		margin-left: 30upx;
		margin-right: 30upx;
		width: 100%;
		height: 100upx;
		font-size: 30upx;
	}

	.image_scroll_content {
		height: 120upx;
		width: 100vw;
		margin-top: 20upx;
	}

	.image {
		width: 120upx;
		height: 120upx;
		min-width: 120upx;
		margin-right: 20upx;
	}

	.submit_button_content {
		display: flex;
		justify-content: center;
		align-items: center;
		background-color: $uni-global-green-color;
		color: #fff;
		font-size: 30upx;
		height: 100upx;
		border-radius: 6upx;
		margin-right: 30upx;
		margin-left: 30upx;
		padding-left: 30upx;
		padding-right: 30upx;
		margin-top: 50upx;
	}
	
	.reply_emoji_button {
		margin-top: 20upx;
		margin-bottom: 20upx;
		margin-left: 30upx;
		width: 60upx;
		height: 60upx;
	}

</style>
